package com.peng.leetcode.tree;


import java.util.*;

/**
 * ListOfDepth
 *
 * @author: lupeng6
 * @create: 2021/2/4 17:17
 */
public class ListOfDepth {

    public static void main(String[] args) {

    }

    public ListNode[] listOfDepth(TreeNode tree) {
        List<ListNode> answer = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(tree);

        while (!queue.isEmpty()) {
            ListNode dump = new ListNode(-1);
            ListNode curr = dump;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                curr.next = new ListNode(node.val);
                curr = curr.next;
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            answer.add(dump.next);
        }
        return answer.toArray(new ListNode[0]);
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
